import { Component, OnInit, ViewChild, AfterViewInit } from "@angular/core";
import { NavigationEnd, Router } from "@angular/router";
import { filter } from "rxjs";
import { DialogService } from 'ng-devui/modal';
import { MintComponent } from '../manage/mint/mint.component'
import { UserManageComponent } from '../manage/user-manage/user-manage.component'
import { CollectionComponent } from '../manage/collection/collection.component'

@Component({
  selector: "app-manage",
  templateUrl: "./manage.component.html",
  styleUrls: ["./manage.component.scss"],
})
export class ManageComponent implements OnInit {
  selectedItem = { title: "集合管理", id: "collection" };
  tab = location.hash.split('/');
  tabActiveId: string | number = this.tab[this.tab.length - 1];
  @ViewChild(MintComponent)
  public Mint!: MintComponent
  @ViewChild(UserManageComponent)
  public userManage!: UserManageComponent
  @ViewChild(CollectionComponent)
  public collection!: CollectionComponent

  menuArray = [
    {
      title: "藏品管理",
      id: "collection",
    },
    {

      title: "铸造",
      id: "mint",
    },
    {
      title: "用户管理",
      id: "userManage",
    },

  ]

  constructor(public router: Router, private dialogService: DialogService) {
  }

  ngOnInit(): void {
    if (localStorage.getItem('PasswordChange') == 'false') {
      this.openDialog('warning', '管理员首次登录，请修改密码，否则无法进行管理操作！');
      this.router.navigate(['/baseUserInfo']);
    }
  }

  changeTab() {
    var tab = {
      title: "藏品管理",
      id: "collection",
    }
    this.tabActiveId = tab.id
    this.selectedItem = tab;
    this.router.navigate([`/manage/${tab.id}`]);
    this.collection.ngOnInit()
  }

  activeTabChange(tab) {
    this.selectedItem = tab;
    this.router.navigate([`/manage/${tab}`]);
    if (tab == 'collection') {
      this.collection.ngOnInit()
    }
    if (tab == 'mint') {
      this.Mint.ngOnInit()
    }
    if (tab == 'userManager') {
      this.userManage.ngOnInit()
    }
  }

  config = {
    id: 'dialog-service',
    width: '346px',
    maxHeight: '600px',
    zIndex: 1050,
    backdropCloseable: true,
    html: true,
  }

  openDialog(dialogtype?: string, content?: string) {
    const results = this.dialogService.open({
      ...this.config,
      content: content,
      dialogtype: dialogtype,
      buttons: [
        {
          text: '关闭',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
          cssClass: 'primary',
        },
      ],
    });
  }
}
